package com.cartoon.pictures.business.common;

import android.os.SystemClock;
import android.util.SparseArray;

import com.catoon.corelibrary.common.Utils;

/**
 * 监控工具
 * Created by chenxunlin01 on 2016/12/20.
 */

public class Monitor {
    public static final String TAG = "Monitor";

    private static SparseArray<MethodInfo> optionStartTimes = new SparseArray<>();

    public static void start(int key, String fileName) {
        MethodInfo methodInfo = new MethodInfo();
        methodInfo.setStartTime(SystemClock.currentThreadTimeMillis());
        methodInfo.setMethodName(methodName(fileName));
        optionStartTimes.put(key, methodInfo);
    }

    public static void resultSize(int key, long size) {
        MethodInfo methodInfo = optionStartTimes.get(key);
        if (methodInfo == null) {
            return;
        }
        methodInfo.setResultSize(size);
    }

    public static void stop(int key) {
        MethodInfo methodInfo = optionStartTimes.get(key);
        if (methodInfo == null) {
            return;
        }
        methodInfo.setEndTime(SystemClock.currentThreadTimeMillis());
    }

    public static String methodName(String fileName) {
        String method = "";
        StackTraceElement[] eles = Thread.currentThread().getStackTrace();
        for (StackTraceElement ele : eles) {
            if (Utils.equal(fileName, ele.getFileName())) {
                method = ele.getMethodName();
            }
        }
        return method;
    }
}
